//
//  YueyouRequireTableCell.m
//  MiaoTu
//
//  Created by dllo on 16/9/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YueyouRequireTableCell.h"
#import "YueyouCollectionCell.h"

#define kYueyouCollectionCell @"YueyouCollectionCellIdentifier"

@interface YueyouRequireTableCell ()
<
    UITextViewDelegate,
    UICollectionViewDataSource,
    UICollectionViewDelegate,
    UICollectionViewDelegateFlowLayout
>
@property (weak, nonatomic) IBOutlet UITextView *requireTextView;
@property (weak, nonatomic) IBOutlet UILabel *requireLabel;
@property (weak, nonatomic) IBOutlet UILabel *requireCountLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *fl;
@property (nonatomic, strong) NSArray *labelArray;
@end

@implementation YueyouRequireTableCell

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
    [self.contentView bringSubviewToFront:_requireLabel];
    [self.contentView bringSubviewToFront:_requireCountLabel];
    self.labelArray = @[@"颜值高", @"技术宅", @"土豪", @"萝莉", @"暖男", @"逗逼", @"不限", @"御姐", @"大叔"];
    [_collectionView registerClass:[YueyouCollectionCell class] forCellWithReuseIdentifier:kYueyouCollectionCell];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _requireTextView.layer.cornerRadius = 4;
    _requireTextView.layer.masksToBounds = YES;
    _requireTextView.layer.borderColor = [UIColor colorWithWhite:0.941 alpha:1.000].CGColor;
    _requireTextView.layer.borderWidth = 1;
}

- (void)textViewDidChange:(UITextView *)textView {
    if (_requireTextView.text.length == 0) {
        _requireLabel.text = @"简述约伴要求或通过下面的标签快速选择";
    } else {
        _requireLabel.text = @"";
    }
    _requireCountLabel.text = [NSString stringWithFormat:@"%ld/30", (unsigned long)textView.text.length];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _labelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YueyouCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kYueyouCollectionCell forIndexPath:indexPath];
    cell.tagLabel.text = _labelArray[indexPath.item];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGRect newRect = [_labelArray[indexPath.row] boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 35) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];
    newRect.size.height += 2;
    newRect.size.width += 10;
    return newRect.size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    _requireTextView.text = [NSString stringWithFormat:@"%@ %@", _requireTextView.text, _labelArray[indexPath.item]];
    [self textViewDidChange:_requireTextView];
}

@end
